//index.js
//获取应用实例
var app = getApp()
var num;
// 84026b6239decc0a7b87272f8365a129
// f73b417d9bb377de5dd0e9adcf5524bf
Page({
  data: {
     dataArr:[],
     navbar: ['推荐', '国内', '娱乐','体育','军事','科技','财经','游戏'],
     num:0,
    currentTab: 0
  },
  navbarTap: function(e){
  num=e.currentTarget.dataset.idx
  console.log(num)
    this.setData({
      currentTab: e.currentTarget.dataset.idx,
      num:num,
    })
    console.log(this.data)
    this.onLoad();
  },
    //获取到图片的个数
  imageCount: function(obj) {
      if ("thumbnail_pic_s03" in obj) {
        return [obj.thumbnail_pic_s,obj.thumbnail_pic_s02,obj.thumbnail_pic_s03];  
      }
      if ("thumbnail_pic_s02" in obj) {
          return [obj.thumbnail_pic_s,obj.thumbnail_pic_s02];
      }
      if ("thumbnail_pic_s" in obj){
         return [obj.thumbnail_pic_s];
      }
  },
  onLoad: function () {
    this.requestHTTP();
  },
  //获取新闻链接地址
  geturlPath: function (url) {
      var  index = url.indexOf("mobile/");
      if (index > 0) {
        url = url.substring(index);
        return   url.replace(".html","");
      }
  },
  requestHTTP: function() {
    var that = this
    var types=new Array()
    types= ['top','guonei','yule','tiyu','junshi','keji','caijing','youxi'];
    var size=30
    console.log(types[num])
    wx.request({
        url: 'https://v.juhe.cn/toutiao/index',
        data: {
          'type':types[num],
          'key':"f73b417d9bb377de5dd0e9adcf5524bf",
          'page_size':size
        },
        method: 'get',
        success: function(res){
          const dataArrs = res.data.result.data
          for(var i = 0; i < dataArrs.length; i++) {
              //  console.log(i);
               var obj = dataArrs[i];
               obj.countArrs = that.imageCount(obj);
               obj.httpurl = that.geturlPath(obj.url);
               that.globalData.tempArrs.push(obj);
          }
          var tempArr=that.globalData.tempArrs
          if(tempArr.length / size >=2){
              that.setData({
                dataArr:tempArr.slice(-size)
              })
          }else{
          that.setData({
             dataArr: tempArr,
          })
        }
        }
    })
  },
  globalData: {
    tempArrs: []
  },
  jump(){
    wx.navigateTo({
      url: '../content/content',
    })
  }
})
